<template>
  <div class="app-container">
    <el-form class="searchFormClass" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="配置名称" prop="hcName">
        <el-input
          v-model="queryParams.hcName"
          placeholder="请输入配置名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Hadoop版本" prop="hadoopVersion" label-width="100px">
        <el-input
          v-model="queryParams.hadoopVersion"
          placeholder="请输入Hadoop版本"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Hadoop是否HA" prop="isHa" label-width="120px">
        <el-select v-model="queryParams.isHa" placeholder="请选择Hadoop是否HA" clearable size="small" @change="handleQuery()">
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dmphadoop:config:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dmphadoop:config:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dmphadoop:config:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['dmphadoop:config:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="configList" @row-dblclick="showForm" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" width="55" align="center" prop="hcId" />
      <el-table-column label="配置名称" align="center" prop="hcName" />
      <el-table-column label="Hadoop版本" align="center" prop="hadoopVersion" />
      <el-table-column label="Hadoop是否HA" align="center" prop="isHa">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isHa"/>
        </template>
      </el-table-column>
      <el-table-column label="非HA配置fs.defaultFS" align="center" prop="nohaFsDefaultfs" />
      <el-table-column label="HA配置dfs.nameservices" align="center" prop="haDfsNameservices" />
      <el-table-column label="HA配置实际第一个NameNode地址" align="center" prop="haDfsNamenode1Address" />
      <el-table-column label="HA配置实际第二个NameNode地址" align="center" prop="haDfsNamenode2Address" />
      <el-table-column label="操作" align="center" fixed="right" width="110" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dmphadoop:config:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dmphadoop:config:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改Hadoop配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal="false" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="250px">
        <el-form-item label="配置名称" prop="hcName">
          <el-input v-model="form.hcName" placeholder="请输入配置名称" />
        </el-form-item>
        <el-form-item label="Hadoop版本" prop="hadoopVersion">
          <el-input v-model="form.hadoopVersion" placeholder="请输入Hadoop版本" />
        </el-form-item>
        <el-form-item label="Hadoop是否HA" prop="isHa">
          <el-select v-model="form.isHa" placeholder="请选择Hadoop是否HA"  @change="haChange">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="非HA配置fs.defaultFS" prop="nohaFsDefaultfs">
          <el-input v-model="form.nohaFsDefaultfs" :disabled="nohaEdit" placeholder="请输入非HA配置fs.defaultFS" />
        </el-form-item>
        <el-form-item label="HA配置dfs.nameservices" prop="haDfsNameservices">
          <el-input v-model="form.haDfsNameservices" :disabled="haEdit" placeholder="请输入HA配置dfs.nameservices" />
        </el-form-item>
        <el-form-item label="HA配置实际第一个NameNode地址" prop="haDfsNamenode1Address">
          <el-input v-model="form.haDfsNamenode1Address" :disabled="haEdit" placeholder="请输入HA配置实际第一个NameNode地址" />
        </el-form-item>
        <el-form-item label="HA配置实际第二个NameNode地址" prop="haDfsNamenode2Address">
          <el-input v-model="form.haDfsNamenode2Address" :disabled="haEdit" placeholder="请输入HA配置实际第二个NameNode地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="isShowConfimBtn" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style scoped>
  .el-form-item {
    margin-bottom: 16px; /*控制行高*/
  }
  .searchFormClass .el-input {
    width: 160px;
  }
</style>

<script>
import { listConfig, getConfig, delConfig, addConfig, updateConfig } from "@/api/dmphadoop/config";

export default {
  name: "Config",
  dicts: ['sys_yes_no'],
  data() {
    return {
      nohaEdit: false,
      haEdit: false,
      // 是否显示确定/提交按钮,默认true显示
      isShowConfimBtn: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // Hadoop配置表格数据
      configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hcName: null,
        hadoopVersion: null,
        isHa: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        hcName: [
          { required: true, message: "配置名称不能为空", trigger: "blur" }
        ],
        hadoopVersion: [
          { required: true, message: "Hadoop版本不能为空", trigger: "blur" }
        ],
        isHa: [
          { required: true, message: "Hadoop是否HA不能为空", trigger: "change" }
        ],
        createTime: [
          { required: true, message: "创建日期不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询Hadoop配置列表 */
    getList() {
      this.loading = true;
      listConfig(this.queryParams).then(response => {
        this.configList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        hcId: null,
        hcName: null,
        hadoopVersion: null,
        isHa: null,
        nohaFsDefaultfs: null,
        haDfsNameservices: null,
        haDfsNamenode1Address: null,
        haDfsNamenode2Address: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.hcId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加Hadoop配置";
      this.isShowConfimBtn = true;// 显示确定按钮
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const hcId = row.hcId || this.ids
      getConfig(hcId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改Hadoop配置";
        this.isShowConfimBtn = true;// 显示确定按钮
      });
    },
    /** 查看操作 */
    showForm(row) {
      this.reset();
      const hcId = row.hcId || this.ids
      getConfig(hcId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看Hadoop配置";
        this.isShowConfimBtn = false;// 隐藏确定按钮
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.hcId != null) {
            updateConfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const hcIds = row.hcId || this.ids;
      this.$modal.confirm('是否确认删除Hadoop配置编号为"' + hcIds + '"的数据项？').then(function() {
        return delConfig(hcIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dmphadoop/config/export', {
        ...this.queryParams
      }, `config_${new Date().getTime()}.xlsx`)
    },
    /** Hadoop是否HA下拉项改变事件*/
    haChange(val){
      if('Y'==val){
        // 清空值
        this.form.nohaFsDefaultfs='';
        // 禁止输入
        this.nohaEdit = true;
        this.haEdit = false;
      }else{
        // 清空值
        this.form.haDfsNameservices='';
        this.form.haDfsNamenode1Address='';
        this.form.haDfsNamenode2Address='';
        // 禁止输入
        this.nohaEdit = false;
        this.haEdit = true;
      }
    },


  }
};
</script>
